<?php

namespace app\admin\model;


use think\Model;

class Extend extends Model {

    // 设置当前模型对应的完整数据表名称
    protected $table = '__AUTH_EXTEND__';

    public static $TYPE_USER = 10;
    public static $TYPE_MENU = 20;
    public static $TYPE_DATA = 30;
    public static $TYPE_DATA_DETAIL = 31;

    public static function listMenus($group_id, $module) {

        $menu_list = Menu::where('status', 1)->where('module', $module)->where('is_dev', 0)
            ->field('id,pid,title')->order('sort')->select();
        $auth_ids = self::where('type', self::$TYPE_MENU)->where('group_id', $group_id)->column('extend_id');
        for ($i = 0; $i < count($menu_list); $i++) {
            $menu_list[$i]['checked'] = '';
            foreach ($auth_ids as $auth_id) {

                if ($menu_list[$i]['id'] == $auth_id) {
                    $menu_list[$i]['checked'] = 'checked';
                    break;
                }
            }
        }
        return $menu_list;
    }

    public static function listAuthIds($user_id, $type) {
        $auth_ids = self::alias('a')->join('__AUTH_EXTEND__ b', 'a.group_id=b.group_id')
            ->where('b.extend_id', $user_id)
            ->where('a.type', $type)
            ->where('b.type', self::$TYPE_USER)
            ->column('a.extend_id');
        return array_unique($auth_ids);
    }


    public static function listAuthMenus($user_id, $module) {
        $menus = Menu::alias('a')
            ->where(array(['a.status', '=', 1], ['a.module', '=', $module], ['is_dev', '=', '0']))
            ->field('a.id,a.title, a.url')->order('a.sort')->select();

        if (!is_admin()) {
            $auth_ids = self::alias('a')->join('__AUTH_EXTEND__ b', 'a.group_id=b.group_id')
                ->where('b.extend_id', $user_id)
                ->where('a.type', self::$TYPE_MENU)
                ->where('b.type', self::$TYPE_USER)
                ->column('a.extend_id');
            $auth_ids = array_unique($auth_ids);
            for ($i = 0; $i < count($menus); $i++) {
                $flag = false;
                foreach ($auth_ids as $auth_id) {
                    if ($menus[$i]['id'] == $auth_id) {
                        $flag = true;
                        break;
                    }
                }
                if ($flag == false) {
                    unset($menus[$i]);
                }

            }

        }
        return $menus->toArray();
    }

    public static function listDatas($group_id) {
        $office_list = Office::where('status', 1)->field('id,title')->order('sort')->select();
        $auth_ids = self::where('type', self::$TYPE_DATA_DETAIL)->where('group_id', $group_id)->column('extend_id');
        for ($i = 0; $i < count($office_list); $i++) {
            $office_list[$i]['checked'] = '';
            foreach ($auth_ids as $auth_id) {
                if ($office_list[$i]['id'] == $auth_id) {
                    $office_list[$i]['checked'] = 'checked';
                    break;
                }
            }
        }
        return $office_list;
    }

    public static function listUsers($group_id) {
        return self::alias('a')
            ->join('__SYS_MEMBER__ b', 'a.extend_id = b.id')
            ->join('__SYS_OFFICE__ c', 'b.office_id = c.id')
            ->where('b.status', 1)
            ->where('a.type', self::$TYPE_USER)
            ->where('a.group_id', $group_id)
            ->field('b.*, c.title as office_title')
            ->order('a.id desc')->paginate(10);

    }


    public static function getDataType($group_id) {
        return self::where('group_id', $group_id)->where('type', self::$TYPE_DATA)->value('extend_id');
    }

    public static function add($group_id, $extend_id, $type) {
        $data = ['group_id' => $group_id, 'extend_id' => $extend_id, 'type' => $type];
        return self::insert($data);
    }

    public static function flush($group_id, $data_ids, $type) {
        self::where('group_id', $group_id)->where('type', $type)->delete();
        $data = [];
        foreach ($data_ids as $menu_id) {
            array_push($data, ['group_id' => $group_id, 'extend_id' => $menu_id, 'type' => $type]);
        }
        return self::insertAll($data);
    }

    public static function remove($group_id, $extend_id, $type) {
        return self::where('group_id', $group_id)->where('extend_id', $extend_id)->where('type', $type)->delete();
    }

    public static function removeByExtend($extend_id, $type) {
        return self::where('extend_id', $extend_id)->where('type', $type)->delete();
    }


    public static function listAuthDatas($user_id) {
        $list = self::alias('a')
            ->join('__AUTH_EXTEND__ b', 'a.group_id=b.group_id and b.type=1')
            ->where('b.extend_id', $user_id)
            ->where('a.type', 'like', '3%')
            ->field('a.extend_id, a.type')->order('a.type')
            ->select();
        $data = [];
        if (!empty($list)) {
            $data['data_type'] = $list[0]['extend_id'];

            if ($data['data_type'] == 3) {
                $data['data_detail'] = [];
                for ($i = 1; $i < count($list); $i++) {
                    array_push($data['data_detail'], $list[$i]['extend_id']);
                }
            } else if ($data['data_type'] == 1) {
                $ids = Office::where('status', 1)->order('sort')->column('id');
                $data['data_detail'] = $ids;
            } else {
                $data['data_detail'] = [];
                array_push($data['data_detail'], CID);
            }
        } else {
            throw new Exception('用户数据权限未授权，请联系系统管理员');
        }
        return $data;
    }
}